package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.result.PageResult;
import com.sky.service.OrderAdminService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderAdminServiceImpl implements OrderAdminService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Override
    public PageResult conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        PageHelper.startPage(ordersPageQueryDTO.getPage(), ordersPageQueryDTO.getPageSize());
        Page<Orders> orders =orderMapper.getOrder(ordersPageQueryDTO);
        List<OrderVO> orderVOList = new ArrayList<>();
        for (Orders order : orders) {
            Long ordersId = order.getId();
            List<OrderDetail> list = orderDetailMapper.getByOrderId(ordersId);
            OrderVO orderVO = new OrderVO();
            Map<String, Integer> map=new HashMap<>();
            for (OrderDetail orderDetail : list) {
                String name = orderDetail.getName();
                Integer number = orderDetail.getNumber();
                map.put(name, number);
            }
            BeanUtils.copyProperties(order, orderVO);
            orderVO.setOrderDishes(map.toString());
            orderVOList.add(orderVO);
        }
        return new PageResult(orders.getTotal(), orderVOList);

    }

    @Override
    public OrderStatisticsVO statistics() {
        OrderStatisticsVO orderStatisticsVO = new OrderStatisticsVO();
        orderStatisticsVO.setConfirmed(orderMapper.countByStatus(Orders.CONFIRMED));
        orderStatisticsVO.setToBeConfirmed(orderMapper.countByStatus(Orders.TO_BE_CONFIRMED));
        orderStatisticsVO.setDeliveryInProgress(orderMapper.countByStatus(Orders.DELIVERY_IN_PROGRESS));
        return orderStatisticsVO;
    }

    @Override
    public void rejection(OrdersRejectionDTO ordersRejectionDTO) {
//        Integer status =Orders.CANCELLED;
        orderMapper.updateById(ordersRejectionDTO);
    }

    @Override
    public void confirm(OrdersConfirmDTO ordersConfirmDTO) {
        Long id = ordersConfirmDTO.getId();
        Orders orders = new Orders();
        orders.setId(id);
        orders.setStatus(Orders.CONFIRMED);
        orderMapper.update2(orders);
    }

    @Override
    public void delivery(Long id) {
        Orders orders = new Orders();
        orders.setId(id);
        orders.setStatus(Orders.DELIVERY_IN_PROGRESS);
        orderMapper.update2(orders);
    }

    @Override
    public void complete(Long id) {
        Orders orders = new Orders();
        orders.setId(id);
        orders.setStatus(Orders.COMPLETED);
        orders.setDeliveryTime(LocalDateTime.now());
        orderMapper.updateStatus(orders);
    }

    @Override
    public void cancel(OrdersCancelDTO ordersCancelDTO) {
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersCancelDTO, orders);
        orders.setStatus(Orders.CANCELLED);
        orders.setCancelTime(LocalDateTime.now());
        orderMapper.updateById2(orders);
    }

    @Autowired
    private UserMapper userMapper;
    @Override
    public BusinessDataVO getTurnoverByStatus(LocalDateTime start, LocalDateTime end) {
        List<Orders> orders =orderMapper.ByStatus(start, end);
        if (orders != null) {
            Double turnover = 0.0;
            for (Orders order : orders) {
                double v = order.getAmount().doubleValue();
                turnover += v;
            }
            Integer validOrderCount = orders.size();
            int count = orderMapper.getCount(6, start, end);
            Double d=(double)(count+validOrderCount);
            Double orderCompletionRate=0.0;
            if (d!=0){
                orderCompletionRate = (double) validOrderCount / d;
            }
            int c=orderMapper.getCount2(start, end);
            Double unitPrice=0.0;
            if (c!=0){
                unitPrice = turnover / c;
            }
            //新增用户数
            int newUsers = userMapper.getCountByTime(start, end);
            BusinessDataVO businessDataVO = new BusinessDataVO();
            businessDataVO.setTurnover(turnover);
            businessDataVO.setValidOrderCount(validOrderCount);
            businessDataVO.setOrderCompletionRate(orderCompletionRate);
            businessDataVO.setUnitPrice(unitPrice);
            businessDataVO.setNewUsers(newUsers);
            return businessDataVO;
        }
        return null;
    }
}
